package org.reyton.service.impl;

import org.reyton.dao.BizClaimVoucherDao;
import org.reyton.dao.BizClaimVoucherDetailDao;
import org.reyton.entity.BizClaimVoucher;
import org.reyton.entity.BizClaimVoucherDetail;
import org.reyton.entity.PageBean;
import org.reyton.entity.SysEmployee;
import org.reyton.service.BizClaimVoucherService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

/**
 * 报销单明细表服务实现类
 * Created by reyton on 16-12-1.
 */
@Service("bizClaimVoucherService")
public class BizClaimVoucherServiceImpl implements BizClaimVoucherService {
    @Resource(name = "bizClaimVoucherDao")
    private BizClaimVoucherDao bizClaimVoucherDao;
    @Resource(name = "bizClaimVoucherDetailDao")
    private BizClaimVoucherDetailDao bizClaimVoucherDetailDao;
    @Override
    @Transactional(propagation = Propagation.REQUIRED,
            isolation = Isolation.DEFAULT,
            rollbackFor = RuntimeException.class)
    @Deprecated
    public void saveClaimVoucherAndDetail(boolean isFirstCreate, BizClaimVoucher bizClaimVoucher, BizClaimVoucherDetail bizClaimVoucherDetail) {
        //如果是第一次创建报销单，那么就新增一张报销单数据
        if (isFirstCreate) {
            bizClaimVoucherDao.save(bizClaimVoucher);
        }
        //否则不创建报销单直接添加报销单明细
        bizClaimVoucherDetail.setBizClaimVoucher(bizClaimVoucher);
        bizClaimVoucherDetailDao.save(bizClaimVoucherDetail);
        /*
        //计算总金额
        int account = 0;
        Iterator<BizClaimVoucherDetail> it = bizClaimVoucher.getBizClaimVoucherDetails().iterator();
        while (it.hasNext()){
            BizClaimVoucherDetail b = it.next();
            account = account +  b.getAccount();
        }
        bizClaimVoucher.setTotalAccount(account);
        */
        bizClaimVoucherDao.updateAccount(bizClaimVoucher);
        bizClaimVoucherDao.updateDes(bizClaimVoucher);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,
            isolation = Isolation.DEFAULT,
            rollbackFor = RuntimeException.class)
    public void saveClaimVoucher(BizClaimVoucher bizClaimVoucher) {
        bizClaimVoucherDao.save(bizClaimVoucher);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,
            isolation = Isolation.DEFAULT,
            rollbackFor = RuntimeException.class)
    public void updateDes(BizClaimVoucher bizClaimVoucher) {
        bizClaimVoucherDao.updateDes(bizClaimVoucher);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,
            isolation = Isolation.DEFAULT,
            rollbackFor = RuntimeException.class)
    public void updateStatus(BizClaimVoucher bizClaimVoucher) {
        bizClaimVoucherDao.updateStatus(bizClaimVoucher);
    }

    @Override
    public PageBean<BizClaimVoucher> findPageById(SysEmployee sysEmployee, int pageSize, int pageIndex) {
        PageBean<BizClaimVoucher> bizClaimVoucherPageBean = new PageBean<BizClaimVoucher>(pageSize,pageIndex,bizClaimVoucherDao.findCountById(sysEmployee));
        bizClaimVoucherPageBean.setList(bizClaimVoucherDao.findPageById(sysEmployee,pageSize,pageIndex));
        return  bizClaimVoucherPageBean;
    }

    @Override
    public PageBean<BizClaimVoucher> findPageByNextDealSn(SysEmployee sysEmployee, int pageSize, int pageIndex) {
        PageBean<BizClaimVoucher> bizClaimVoucherPageBean = new PageBean<BizClaimVoucher>(pageSize,pageIndex,bizClaimVoucherDao.findCountById(sysEmployee));
        bizClaimVoucherPageBean.setList(bizClaimVoucherDao.findByNextDealSn(sysEmployee.getSn(),pageSize,pageIndex));
        return  bizClaimVoucherPageBean;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,
            isolation = Isolation.DEFAULT,
            rollbackFor = RuntimeException.class)
    public void deleteClaimVoucher(BizClaimVoucher bizClaimVoucher) {
        if(bizClaimVoucher.getId() !=  null){
            bizClaimVoucher = bizClaimVoucherDao.findById(bizClaimVoucher.getId());
            bizClaimVoucherDao.delete(bizClaimVoucher);
        }
    }

    @Override
    public BizClaimVoucher findById(BizClaimVoucher bizClaimVoucher) {
        return  bizClaimVoucherDao.findById(bizClaimVoucher.getId());
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,
            isolation = Isolation.DEFAULT,
            rollbackFor = RuntimeException.class)
    public void updateAccount (BizClaimVoucher bizClaimVoucher) {
        bizClaimVoucherDao.updateAccount(bizClaimVoucher);
    }



    public void setBizClaimVoucherDetailDao(BizClaimVoucherDetailDao bizClaimVoucherDetailDao) {
        this.bizClaimVoucherDetailDao = bizClaimVoucherDetailDao;
    }

    public void setBizClaimVoucherDao(BizClaimVoucherDao bizClaimVoucherDao) {
        this.bizClaimVoucherDao = bizClaimVoucherDao;
    }
}
